/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rainbow.udo;

import java.io.File;
import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 *
 * @author hironaga
 */
public class UdoManager {
        
    public enum UConnType {

        FILE(),
        DATABASE(),
        REST();

        private UConnType(){}
    }

    public enum UConnInfo {

        FILE_URI(),
        FILE_PARENT(),
        FILE_CHILD(),
        FILE_PATHNAME();

        private UConnInfo(){}
    }
    
    public enum UQueryType {

        XPATH(),
        QUERY();

        private UQueryType(){}
    }

    public enum UQueryInfo {

        XPATH(),
        XPATH_PARAM(),
        STATEMENT(),
        STATEMENT_PARAM();

        private UQueryInfo(){}
    }
    

    public static UConnection getUConnection(UConnType uType, Map<UConnInfo, Object> uInfo){

        UConnection uconn = null;

        if(uType.equals(UConnType.FILE)){

            File file;

            if(uInfo.get(UConnInfo.FILE_PATHNAME) != null){
                file = new File((String)uInfo.get(UConnInfo.FILE_PATHNAME));
            }
            else if(uInfo.get(UConnInfo.FILE_URI) != null){
                file = new File((URI)uInfo.get(UConnInfo.FILE_URI));
            }
            else if(uInfo.get(UConnInfo.FILE_PARENT) != null){
                Object parent = uInfo.get(UConnInfo.FILE_PARENT);
                if(parent instanceof String){
                    file = new File((String)parent, (String)uInfo.get(UConnInfo.FILE_CHILD));
                }
                else if(parent instanceof File){
                    file = new File((File)parent, (String)uInfo.get(UConnInfo.FILE_CHILD));
                }
                else{
                    throw new UConnectionException("UConnInfo is not enough");
                }
            }
            else {
                throw new UConnectionException("UConnInfo is not enough");
            }

            uconn = new UConnection(file);
        }
        else{
            throw new UConnectionException("UQueryInfo is Unsuported");
        }

        return uconn;
    }

    public static UQuery getUQuery(UQueryType uType, Map<UQueryInfo, Object> uInfo){

        UQuery uquery = null;

        if(uType.equals(UQueryType.XPATH)){

            Xpath xpath;

            if(uInfo.get(UQueryInfo.XPATH) != null
                    && uInfo.get(UQueryInfo.XPATH_PARAM) != null){
                //TODO PARAM
                xpath = new Xpath((String)uInfo.get(UQueryInfo.XPATH),
                        (List<String>)uInfo.get(UQueryInfo.XPATH_PARAM));
            }
            else if(uInfo.get(UQueryInfo.XPATH) != null){
                xpath = new Xpath((String)uInfo.get(UQueryInfo.XPATH));
            }
            else{
                throw new UConnectionException("UQueryInfo is not enough");
            }

            uquery = new UQuery(xpath);
        }
        else{
            throw new UConnectionException("UQueryInfo is Unsuported");
        }

        return uquery;
    }

}
